/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jplurk.api;

import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Jose
 */
public class Clique {
    
    private Long ownerId;
    private String name;
    private ArrayList<Integer> friendIds;
    private ArrayList<Integer> originalFriendIds;

    public Clique(Long ownerId, String name) {
        this.ownerId = ownerId;
        this.name = name;
        this.originalFriendIds = new ArrayList<Integer>(0);
    }
    
    public Clique(JSONObject jsonClique) throws JSONException {
        this.ownerId = jsonClique.getLong("user_id");
        this.name = jsonClique.getString("name");
        this.friendIds = this.parseJsonFriends(jsonClique.getString("friends"));
        this.originalFriendIds = this.friendIds;
    }

    public Clique(Long ownerId, String name, ArrayList<Integer> friendIds) {
        this.ownerId = ownerId;
        this.name = name;
        this.friendIds = friendIds;
        this.originalFriendIds = this.friendIds;
    }

    public JSONObject toJSONObject() throws JSONException {
        
        JSONObject jo = new JSONObject();
        jo.put("name", this.name);
        jo.put("user_id", this.ownerId);
        jo.put("friends", this.friendsToString());
        
        return jo;
    }
    
    public String toPostParamString() {
        
        String s = "";
        // Name of the clique
        s += "name=" + this.name;
        
        // For each enttry in friendsId, we must check if it is 
        // in originalFriendIds. If it is not, we add it to the string
        // surrounded by inverted commas
        s += "ids=[";
        for (Integer i = 0; i < (this.friendIds.size() - 1); i++) {
            
            if ( this.originalFriendIds.contains(this.friendIds.get(i)) ) {
                s += this.friendIds.get(i) + ",";
            }
            else {
                s += "\"" + this.friendIds.get(i) + "\",";
            }
        }
        
        // Last entry
        if ( this.originalFriendIds.contains(this.friendIds.get(this.friendIds.size() - 1)) ) {
                s += this.friendIds.get(this.friendIds.size() - 1) + "]";
        }
        else {
                s += "\"" + this.friendIds.get(this.friendIds.size() - 1) + "\"]";
        }
              
        return s;
    }
    
    public ArrayList<Integer> getFriendIds() {
        return friendIds;
    }

    public void setFriendIds(ArrayList<Integer> friendIds) {
        this.friendIds = friendIds;
    }

    public ArrayList<Integer> getOriginalFriendIds() {
        return originalFriendIds;
    }

    public void setOriginalFriendIds(ArrayList<Integer> originalFriendIds) {
        this.originalFriendIds = originalFriendIds;
    }

    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }

    public <T> T[] toArray(T[] a) {
        return friendIds.toArray(a);
    }

    public int size() {
        return friendIds.size();
    }

    public boolean remove(Object o) {
        return friendIds.remove(o);
    }

    public Integer remove(int index) {
        return friendIds.remove(index);
    }

    public boolean isEmpty() {
        return friendIds.isEmpty();
    }

    public Integer get(int index) {
        return friendIds.get(index);
    }

    public boolean contains(Object o) {
        return friendIds.contains(o);
    }

    public void clear() {
        friendIds.clear();
    }

    public boolean addAll(Collection<? extends Integer> c) {
        return friendIds.addAll(c);
    }

    public boolean add(Integer e) {
        return friendIds.add(e);
    }
    
    protected ArrayList<Integer> parseJsonFriends(String jsonString) {
        Pattern friendIdPattern = Pattern.compile("\\|[^\\|]*\\|");
        Matcher friendIdMatcher = friendIdPattern.matcher(new String());

        ArrayList<Integer> friends = new ArrayList<Integer>();
        String friendIdStr;
        while (friendIdMatcher.find()) {
            friendIdStr = friendIdMatcher.group().replace("|", "");
            friends.add(new Integer(friendIdStr));
        }
        
        return friends;
    }

    protected String friendsToString() {
        
        String s = "";
        
        if (!this.friendIds.isEmpty()) {
            for (Integer id : this.friendIds) {
                s += "|" + id + "|";
            }
        }
        
        return s;
    }

}
